package socket;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 聊天室客户端
 */
public class Client {
    // java套接字
    private Socket socket;

    // 构造方法，用于初始化客户端
    public Client(){
        try{
            // 正在连接服务端
            System.out.println("正在连接服务端...");
            // 参数1：一般都是主机ip地址，第二个参数为主机开放的端口号
//            socket = new Socket("172.40.179.124",8088);
            socket = new Socket("127.0.0.1",8088);
            System.out.println("连接成功");
            System.out.println();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    // 客户端开始工作的方法
    public void start() {
        System.out.println("客户端启动");
        try{
            // 创建一个输出流，用于向服务端发送数据
            // OutputStream 是一个抽象类，低级流
            OutputStream outputStream = socket.getOutputStream();
            // 创建字符转换流
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
            // 创建缓冲流
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            // 创建打印流
            PrintWriter printWriter = new PrintWriter(bufferedWriter,true);
            //
            // outputStream.write(1);
            Scanner scanner = new Scanner(System.in);
            // 首先输入用户名，然后将输入的内容发送给服务端
            System.out.println("请输入用户名：");
            while(true){
                String name = scanner.nextLine();
                if(name.trim().isEmpty()){
                    System.out.println("昵称不能为空");
                }else{
                    printWriter.println(name);
                    break;
                }
            }



    /*        // 接收服务器端发过来的数据
            InputStream inputStream = socket.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            // 创建线程
            Thread getMessage = new Thread(()->{
                while (true){
                    try{
                        String msg = bufferedReader.readLine();
                        System.out.println("服务端返回的数据：" + msg);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });*/
            // 创建线程去单独读取服务端发过来的数据
            ServerHandle getMessage = new ServerHandle();
            // 实例化对象的类继承了Runnable接口，所以可以作为线程对象
            Thread getMessageThread = new Thread(getMessage);
            getMessageThread.start();


            //
            while (true){
                System.out.println("请输入要发送的内容：");
                String msg = scanner.nextLine();

                if("exit".equalsIgnoreCase(msg)){
                    break;
                }
                else{
                    // 向服务端发送数据
                    printWriter.println(msg);
                }
//                String serverMessage = bufferedReader.readLine();
//                System.out.println("服务端返回的数据：" + serverMessage);
//                getMessage.start();
            }

        } catch (Exception e){
            e.printStackTrace();
        }finally{
            try{
                // 关闭流
                socket.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

    // 创建服务端发送过来的消息的
    private class ServerHandle implements Runnable{
//        private Socket socket;

        // 构造器
        public ServerHandle(){
//            this.socket = socket;
        }
        public void run() {
            // 创建字节输入流
            try {
                InputStream inputStream = socket.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String msg;
                while ((msg = bufferedReader.readLine()) !=null){
//                    String msg = bufferedReader.readLine();
                    System.out.println(msg);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }
    public static void main(String[] args) {
        // 创建客户端对象
        Client client = new Client();
        // 启动客户端
        client.start();
    }

}
